define(function(require, exports, module) {

  var _ = require('underscore')
  var createEl = require('../util/createEl')

 var LINE_AWAY_DISTANCE = 16; // 折线绕开node距离
var POINT_CUT_DISTANCE = 5; // edge中连接点裁剪边距或折线裁剪边距


/**
 * point 规定格式 {x:0,y:0}
 * direction 规定格式 's/w/e/n'
 * 返回edge的一些option:
 * edge.el本身的props，还有重新计算过的始终点坐标（基于新的edge坐标系）
 * 
 * @param  {[type]} opts
 * {
 *   source: { point: , direction:''},
 *   target: { point: , direction:''}
 * }
 * @return {[type]}      [description]
 */
function calcEdgeOption(opts) {

  var source = opts.source;
  var target = opts.target;

  var sourcePoint = source.point;
  var targetPoint = target.point;

  return {
    style: {
      x: sourcePoint.x - POINT_CUT_DISTANCE,
      y: sourcePoint.y - POINT_CUT_DISTANCE,
      width: targetPoint.x - sourcePoint.x + 2 * POINT_CUT_DISTANCE,
      height: targetPoint.y - sourcePoint.y + 2 * POINT_CUT_DISTANCE,
    },
    points: [
      [POINT_CUT_DISTANCE, POINT_CUT_DISTANCE],
      [targetPoint.x - sourcePoint.x + POINT_CUT_DISTANCE, targetPoint.y - sourcePoint.y + POINT_CUT_DISTANCE]
    ]
  }
}


  function Edge(el, opts) {
    this.el = el;
    
    this.source = opts.source;
    this.target = opts.target;
    this.points = opts.points;
    this.props = _.extend({
      x: 0,
      y: 0,
      w: 100,
      h: 100,
      rotate: 0
    }, opts.props);
  }

  Edge.init = function(root, id, opts) {
    var source = opts.source;
    var target = opts.target;

    var edgeOption = calcEdgeOption({
      source: {
        point: source.node.getConnectPoint(source.direction),
        direction: source.direction
      },
      target: {
        point: target.node.getConnectPoint(target.direction),
        direction: target.direction
      }
    })
    var el = createEl('div', edgeOption.style)
    el.id = '__edge__' + id;
    el.className = "edge";
    el.setAttribute('title', '__edge__' + id);
    root.appendChild(el);
    var edge = new Edge(el, {
      source: opts.source,
      target: opts.target,
      props: edgeOption.style,
      points: edgeOption.points
    })
    edge.id = id;
    return edge;
  }

  return Edge
})